package com.axc.gallery2.dbhelper;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

/**
 * 本地相册数据库操作辅助类
 *
 */

public class AlbumDBHelper extends DbHelper {

	public AlbumDBHelper(Context context) {
		super(context);
	}

	/**
	 * 获取所有相册
	 * @return
	 */
	public List<AlbumInfo> getAlbumInfors(){
		String sqlString = "select * from "+TABLE_ALBUM_NAME;
		Cursor cursor = super.rawQuery(sqlString, null);
		List<AlbumInfo>  albums = new ArrayList<AlbumInfo>();
		int idIndex =cursor.getColumnIndex(FIELD_ID);
		int nameIndex =cursor.getColumnIndex(TABLE_ALBUM_COLUNM_NAME);
		int drIndex =cursor.getColumnIndex(TABLE_ALBUM_COLUNM_DESCRIPTION);
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
		{
			int idInt =cursor.getInt(idIndex);
			String albumName = cursor.getString(nameIndex);
			String albumDescription = cursor.getString(drIndex);
			albums.add(new AlbumInfo(idInt,albumName,albumDescription,null));
		};
		cursor.close();
		super.colseDBbase();
		return albums;
	}

	/**
	 * 获取相册图片
	 * @param id
	 * @return
	 */
	public List<ImageInfo> getImages(int id){
		String sqlString= MessageFormat.format("SELECT {0} FROM {1} WHERE {2}={3}", 
				TABLE_IMAGES_COLUNM_LOCATION,TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_ALBUMID,String.valueOf(id));
		System.out.println(sqlString);
		Cursor cursor = super.rawQuery(sqlString,null);
		List<ImageInfo> albumNames =new ArrayList<ImageInfo>();
		System.out.println("getImages:cursorsize"+cursor.getCount());
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
		{
			ImageInfo imageInfo=new ImageInfo();
			imageInfo.image_path=cursor.getString(0);
			albumNames.add(imageInfo);
		};
		cursor.close();
		super.colseDBbase();
		/*System.out.println("----------------------");
		System.out.println(albumNames.size());
		System.out.println("----------------------");*/

		return albumNames;
	}
	/**
	 * 获取相册图片
	 * @param name
	 * @return
	 */
	public List<String> getImages(String name){
		String sqlString= MessageFormat.format("SELECT {0} FROM {1} WHERE {2}=(SELECT {2} FROM {3} WHERE {4}=?)", 
				TABLE_IMAGES_COLUNM_LOCATION,TABLE_IMAGES_NAME,FIELD_ID,TABLE_ALBUM_NAME,TABLE_IMAGES_COLUNM_LOCATION);

		Cursor cursor = super.rawQuery(sqlString,new String[]{name});
		List<String> albumNames =new ArrayList<String>();
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
		{
			albumNames.add(cursor.getString(0));
		};
		cursor.close();
		super.colseDBbase();
		return albumNames;
	}

	/**
	 * 获取所有相册对象
	 * @return
	 */
	public List<AlbumInfo> getAlbums(){
		List<AlbumInfo> albumList = this.getAlbumInfors();
		//System.out.println("getAlbums:size"+albumList.size());
		for (int i = 0; i < albumList.size(); i++) {
			List<ImageInfo> albumItems= new ArrayList<ImageInfo>();;
			if(this.getImages(albumList.get(i).getAlbumId()).size()>0){

				albumItems =this.getImages(albumList.get(i).getAlbumId());
			}
			albumList.get(i).setAlbumItems(albumItems);
		}
		return albumList;
	}

	/**
	 * 获取指定相册
	 * @param albumId
	 * @return
	 */
	public AlbumInfo getAlbum(int albumId){
		List<AlbumInfo> albumList =this.getAlbums();
		for (int i = 0; i < albumList.size(); i++) {
			if(albumList.get(i).getAlbumId()==albumId){
				return albumList.get(i);
			}				
		}
		return null;

	}

	/**
	 * 添加相册
	 * @param albumName
	 * @param albumDescription
	 */
	public void addAlbum(String albumName,String albumDescription){
		super.insert(TABLE_ALBUM_NAME, new String[]{TABLE_ALBUM_COLUNM_NAME,TABLE_ALBUM_COLUNM_DESCRIPTION}
		, new String[]{albumName,albumDescription});
	}
	
	/**
	* 修改相册信息
	*/
	public void updateAlbum(int id,String albumName,String description){
		super.update(TABLE_ALBUM_NAME, id, new String[]{FIELD_ID,TABLE_ALBUM_COLUNM_NAME,TABLE_ALBUM_COLUNM_DESCRIPTION},
				new String[]{String.valueOf(id),albumName,description});
	}
	/**
	 * 删除相册
	 */
	public void delAlbum(int albumId){
		super.delete(TABLE_ALBUM_NAME, albumId);
		//删除相册集合中的图片
		ImagesDBhelper imagesDBhelper =new ImagesDBhelper(getContext());
		imagesDBhelper.delByAllbumId(albumId);
	}
	
	/**
	 * 获取最大的id
	 * @return
	 */
	public int getMaxId() {
		db=getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from axc_album order by _id desc", null);
		cursor.moveToFirst();
		int maxId = cursor.getInt(0);
		cursor.close();
		colseDBbase();
		return maxId;
	}

}
